using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace cb0tServer
{
    class UDPChatroomObject : IComparable
    {
        public IPAddress ip;
        public ushort port;
        public int ack_count;
        public int try_count;
        public int last_connect;
        public int last_received;
        public int last_sent_add_IPS;

        public UDPChatroomObject()
        {
            this.ack_count = 0;
            this.try_count = 0;
            this.last_connect = 0;
            this.last_received = 0;
            this.last_sent_add_IPS = 0;
        }

        public UDPChatroomObject(IPAddress ip, ushort port)
        {
            this.ip = ip;
            this.port = port;
            this.ack_count = 0;
            this.try_count = 0;
            this.last_connect = 0;
            this.last_received = 0;
            this.last_sent_add_IPS = 0;
        }

        public IPEndPoint ToIPEndPoint()
        {
            return new IPEndPoint(ip, port);
        }            

        public override bool Equals(object obj)
        {
            UDPChatroomObject c = (UDPChatroomObject)obj;
            return this.ip.Equals(c.ip);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public int CompareTo(object obj)
        {
            int result = 0;

            switch (ServerCore.UDPSortMethod)
            {
                case ServerCore.UDPSortMethodOptions.SortOldestContactedFirst:
                    result = this.last_sent_add_IPS - ((UDPChatroomObject)obj).last_sent_add_IPS;
                    break;

                case ServerCore.UDPSortMethodOptions.SortMostRecentFirst:
                    result = ((UDPChatroomObject)obj).last_connect - this.last_connect;
                    break;

                case ServerCore.UDPSortMethodOptions.SortMostAckedFirst:
                    result = ((UDPChatroomObject)obj).ack_count - this.ack_count;
                    break;
            }

            return result;
        }
    }
}
